#include"d:\bcsource\c\include\graph.h"
#include <conio.h>
#include "d:\bcsource\c\include\mathlab.h"
#define nline 200
#define NY 6 
#define NL 102

int nl=10,place=0,dl=200; 
double y[NL][NY],x[NL][NY]; 
double dy[NY],dx[NY]; 

void drawspline(int type) 
{ 
int ty[nline],tx0,ty0,tx1,ty1,i,tplace=(place+1)%nl; 
double poly[4][NY][4]; 
makepoly(x[place],NY,poly[0]); 
makepoly(y[place],NY,poly[1]); 
makepoly(x[tplace],NY,poly[2]); 
makepoly(y[tplace],NY,poly[3]); 
tx0=(int)spline(poly[0],NY,0); 
ty0=(int)spline(poly[1],NY,0); 
tx1=(int)spline(poly[2],NY,0); 
ty1=(int)spline(poly[3],NY,0); 
moveto((int)spline(poly[0],NY,0),(int)spline(poly[1],NY,0)); 
for(i=0;i<=nline;i++) 
{ 
setcolor(0); 
moveto(tx1,ty1); 
tx1=(int)spline(poly[2],NY,(double)i/nline*(NY)); 
ty1=(int)spline(poly[3],NY,(double)i/nline*(NY)); 
lineto(tx1,ty1); 
setcolor(1); 
moveto(tx0,ty0); 
tx0=(int)spline(poly[0],NY,(double)i/nline*(NY)); 
ty0=(int)spline(poly[1],NY,(double)i/nline*(NY)); 
lineto(tx0,ty0); 
} 
/*setcolor(0); 
moveto(tx1,ty1); 
tx1=(int)spline(poly[2],NY,0); 
ty1=(int)spline(poly[3],NY,0); 
lineto(tx1,ty1); 
setcolor(1); 
moveto(tx0,ty0); 
tx0=(int)spline(poly[0],NY,0); 
ty0=(int)spline(poly[1],NY,0); 
lineto(tx0,ty0);*/ 
if(type) 
{ 
for(i=0;i<NY;i++) 
{ 
setcolor(0); 
circle(x[tplace][i],y[tplace][i],i+5); 
setcolor(1); 
circle(x[place][i],y[place][i],i+5); 
} 
} 
} 

void chdxdy(); 
void updtpoints(); 
void chco(int co[][2]) 
{ 
int i; 
for(i=0;i<3;i++) 
if(co[i][0]>=63||co[i][0]<=0) 
co[i][1]=-co[i][1]; 
} 
void updtco(int co[][2]) 
{ 
int i; 
for(i=0;i<3;i++) 
co[i][0]+=co[i][1]; 
chco(co); 
} 


void main() 
{ 
int gd=DETECT,gm,i,mx=0,my=0,mb=0,type=0,j,co[3][2]={{0,1},{20,1},{40,1}},isgoon=1; 
registerbgidriver(EGAVGA_driver);
initgraph(&gd,&gm,""); 
readm(&mx,&my,&mb); 
initrand(); 
for(i=0;i<NY;i++) 
y[place][i]=randto(100,380),x[place][i]=randto(100,540), 
dy[i]=randto(1,2),dx[i]=randto(1,2); 
for(i=0;i<NL;i++) 
for(j=0;j<NY;j++) 
{ 
x[i][j]=20; 
y[i][j]=20; 
} 
{ 
int tco=randto(0,60); 
for(i=0;i<3;i++) 
co[i][0]=(co[i][0]+tco)%63; 
setregscolor(1,co[0][0],co[1][0],co[2][0]); 
} 
i=0; 
setcolor(1); 
outtextxy(10,10,"spline"); 
while(isgoon) 
{ 
int tx=0,ty=0,tb=0; 
readm(&tx,&ty,&tb); 
if(tx!=mx||ty!=my||tb!=mb)break; 
updtpoints(); 
drawspline(type); 
{ 
int t=0; 
for(t=0;t<20;t++) 
delay(dl); 
} 
i++; 
if(i==50) 
{ 
setregscolor(1,co[0][0],co[1][0],co[2][0]); 
updtco(co); 
i=0; 
} 
if(kbhit()) 
{ 
int c,speed,num; 
c=getch(); 
switch(c) 
{ 
case 's': 
printf("\n\nspeed?(1->100)"); 
scanf("%d",&speed); 
if(speed>100)type=1,speed=100; 
else if(speed<1)type=0,speed=1; 
dl=100+10000/(speed+10); 
printf("line numbers?(1->100)"); 
scanf("%d",&num); 
if(num>100)num=100; 
else if(num<1)num=1; 
nl=num+1; 
setfillstyle(1,0); 
bar(0,0,640,480); 
setcolor(1); 
outtextxy(10,10,"spline"); 
break; 
default:isgoon=0;break; 
} 
} 
} 
closegraph(); 
} 


void chdxdy() 
{ 
int i; 
for(i=0;i<NY;i++) 
{ 
if(x[place][i]>560) 
dx[i]=-randto(2,10); 
else if(x[place][i]<80) 
dx[i]=randto(2,10); 
if(y[place][i]>400) 
dy[i]=-randto(2,10); 
else if(y[place][i]<80) 
dy[i]=randto(2,10); 
} 
} 
void updtpoints() 
{ 
int i; 
place++; 
place=place%nl; 
for(i=0;i<NY;i++) 
{ 
x[place][i]=x[(place+nl-1)%nl][i]+dx[i]; 
y[place][i]=y[(place+nl-1)%nl][i]+dy[i]; 
} 
chdxdy(); 
} 